package com.hzdesk.media.koreabatchmatvector.utils;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;

import java.io.File;

public class SDCardUtils {
	private long blockSize;
	private long totalBlocks;
	private long totalSize;
	private long availableBlocks;
	private long availableSize;
	private StatFs stat;
	private Context context;

	public boolean isSdSafe() {
		/**
		 * 当equals结果为true时，代表sd卡可正常使用
		 */
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
	public SDCardUtils(Context context, String path) {
		try {
			stat = new StatFs(path);
			blockSize = stat.getBlockSize();
			totalBlocks = stat.getBlockCount();
			totalSize = blockSize * totalBlocks;
			availableBlocks = stat.getAvailableBlocks();
			availableSize = blockSize * availableBlocks;
			this.context=context;
		} catch (Exception e) {
			Log.e("tag","mag Exception"+e);
		}

	}

	// 获取sd卡总大小
	public String getTotalSize() {
		return Formatter.formatFileSize(context, totalSize);
	}

	// 获取sd卡的可用大小
	public String getAvailableSizeString() {
		// 格式化文件大小的格式
		return Formatter.formatFileSize(context, availableSize);
	}

	public long getExternalSize() {
		return totalSize;
	}

	public long getExternalAvailableSize(Context context, String path) {
		// 格式化文件大小的格式
		return availableBlocks;
	}

	public float getPercentage() {
		return (float)(totalSize-availableSize) / totalSize;
	}
	
	public float getAvailablePercentage() {
		return (float)availableSize / totalSize;
	}
	
	public long getAvailableSize() {
		return availableSize;
	}

	// 获取sd卡根目录字符串的路径
	public String getSdPath() {
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	public File getSdFile() {
		return Environment.getExternalStorageDirectory();
	}
	
	

}
